/**********************************************************************
gc_9_structural_2nd_stage_basic.do

basic model with homogeneous firms.

**********************************************************************/
**********
* SET UP *
**********
clear all
set matsize 2000
set more 1

* location for dofiles *
cd "T:\_Projet_4915\dofiles"
global dir "`c(pwd)'"
cd $dir

set seed 8397740

sysdir set PERSONAL "T:\_Projet_4915\ado"

* specify file locations *
global project_folder "\_Projet_4915"
global data_folder "\_Projet_4915\DATA"
global output_folder "\_Projet_4915\ResultsFolder"
global temp "temp"

local datadir T:\${data_folder}\

*********
* GATES *
*********
* Specify which data you want to work with (synthetic = syn, real = rl) *
local ext = "rl"

* Specify which gender to process (men = men) *
local gender = "men"

* Specify first observed age (24 25 26) *
local a = 25

* Specify occupational definition to use (occ_version 1 2)
local v = 2

* Specify firm class definition (1 2 3 4 5 6)
local d = 6

* Specify the number of unobservable types *
global types  "5"

* Specify earnings data generating process *
global dgps "2"

* STEP 1: second stage - forward simulation *
local gate1 = 1

* STEP 2: second stage - obtain utility parameters *
local gate2 = 1

* STEP X: delete all intermediate datasets *
local gateX = 1

*******************************************
* MAKE A GLOBAL VARIABLE FOR TODAY'S DATE *
*******************************************
local tyr = substr("$S_DATE",8,4)
local tmo = substr("$S_DATE",4,3)
local tmd = trim(substr("$S_DATE",1,2))

* make day of month two digits *
local wl = length("`tmd'")
if `wl'==1 {
local tmd2 ="0"+"`tmd'"
}
if `wl'!=1 {
local tmd2 ="`tmd'"
}

* get numeric month, make it two digits *
local di="`tmd2'"+"`tmo'"+"`tyr'"
local edate = date("`di'", "DMY")
local mono = month(`edate')
local ml = length("`mono'")
if `ml'==1 {
local mono2 ="0"+"`mono'"
}
if `ml'!=1 {
local mono2 ="`mono'"
}

* put final date together *
global date = "`tyr'"+"`mono2'"+"`tmd2'"

****************************************
* DEFINE LITTLE PROGRAMS TO PRINT TIME *
****************************************
program define starttime
	display "Started processing at $S_TIME on $S_DATE"
end

program define endtime
	display "Finished processing at $S_TIME on $S_DATE"
end

************************
* START OF THE PROGRAM *
************************
foreach y_dgp of global dgps ///
{
foreach n_types of global types ///
{

* start log file *
quietly capture log close
quietly log using gc_9_structural_2nd_stage_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'_`ext', text replace

*********************************************
* STEP 1: second stage - forward simulation *
*********************************************
disp "***** Started processing STEP 1 *****"
starttime
if 1 == `gate1' ///
{
	disp "***** STEP 1: second stage - forward simulation *****"
	
	********************************************************************************************
	* STEP 1.0: SAVE A COPY OF ALL STATE VARIABLES USED TO CALCULATE CCPS AND WAGE PARAMETERS  *
	********************************************************************************************
	disp "***** STEP 1.1: SAVE A COPY OF ALL STATE VARIABLES USED TO CALCULATE CCPS AND WAGE PARAMETERS  *****"
	disp "***** Time is now $S_TIME *****"
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_analyze_me_tmp_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	rename state state_index
	merge m:1 pid_int state_index using "`datadir'gc_structural_Qs_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	rename state_index state
	
	global keep_vars ""
	foreach var in ///
	"action" "Laction" "age" /// 
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		gen `var'_index = `var'
		global keep_vars "$keep_vars `var'_index"
	}
	global keep_vars "pid_int state q $keep_vars"
	keep $keep_vars
	order $keep_vars

	local datadir T:\${data_folder}\
	disp "`datadir'"
	compress
	save "`datadir'gc_structural_2nd_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace
	clear
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_2nd_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
		
	*************************************************
	* STEP 1.1: FORWARD SIMULATE CCPS AND EARNINGS  *
	*************************************************
	disp "***** STEP 1.1: FORWARD SIMULATE CCPS AND EARNINGS *****"
	disp "***** Time is now $S_TIME *****"
	
	*****************************
	* STEP 1.1.1: DRAW ACTION j *
	*****************************
	disp "***** STEP 1.1.1: DRAW ACTION j *****"
	disp "***** Time is now $S_TIME *****"
	
	* Create synthetic variables *
	foreach var in ///
	"action" "Laction" "age" /// 
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* construct synthetic aggregate experience variables *
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}		
	
	local counter = 1
	
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"10" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}

	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP10 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Pile up the CCPs into a sort of cdf *
	gen a = CCP10
	gen b = a + CCP20
	gen c = b + CCP30
	gen d = c + CCP90
	
	* Draw uniform (0,1) *
	gen draw = runiform()
	
	* Draw action *
	gen zaction_j = 0
	replace zaction_j = 10 if draw < a
	replace zaction_j = 20 if draw >= a & draw < b
	replace zaction_j = 30 if draw >= b & draw < c
	replace zaction_j = 90 if draw >= c & draw <= 1
	
	* Pick the relevant CCPs *
	gen zCCP_j = .
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace zCCP_j = CCP`act' if zaction_j == `act'
	}
	
	drop CCP*
	
	* Bring ln_y_hat matrix using appropriate STER files from first stage *
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		disp "***** Now estimating parameters of the wage equation for action: `act' *****"
		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict ln_y_hat_`act'
		estimates clear
	}
	
	* Pick the relevant ln_y_hats *
	gen zln_y_hat_j = 0
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace zln_y_hat_j = ln_y_hat_`act' if zaction_j == `act'
	}
	replace zln_y_hat_j = ln(10400) if zaction_j == 90
	drop ln_y_hat*

	* Drop all synthetic variables *
	keep $keep_vars z*
	
	******************************
	* STEP 1.1.2: DRAW ACTION j' *
	******************************
	disp "***** STEP 1.1.2: DRAW ACTION j' *****"
	disp "***** Time is now $S_TIME *****"
	
	* Create synthetic variables *
	foreach var in ///
	"action" "Laction" "age" /// 
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* Forward simulate synthetic variables *
	replace Laction = zaction_j
	replace age = age + 1

	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace exper_`act' = exper_`act' + 1 if zaction_j == `act'
	}
	
	* construct synthetic aggregate experience variables *
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}
	
	local counter = 1
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"10" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}
	
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP10 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	
	drop CCP_TOT
	
	* Pile up the CCPs into a sort of cdf *
	gen a = CCP10
	gen b = a + CCP20
	gen c = b + CCP30
	gen d = c + CCP90
	
	* Draw uniform (0,1) *
	gen draw = runiform()
	
	* Draw action j' *
	gen zaction_jp = 0
	replace zaction_jp = 10 if draw < a
	replace zaction_jp = 20 if draw >= a & draw < b
	replace zaction_jp = 30 if draw >= b & draw < c
	replace zaction_jp = 90 if draw >= c & draw <= 1
	
	* Pick the relevant CCPs *
	gen zCCP_jp = .
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace zCCP_jp = CCP`act' if zaction_jp == `act'
	}
	
	drop CCP*

	* Bring ln_y_hat matrix using appropriate STER files from first stage *
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		disp "***** Now estimating parameters of the wage equation for action: `act' *****"
		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict ln_y_hat_`act'
		estimates clear
	}
	
	* Pick the relevant ln_y_hats *
	gen zln_y_hat_jp = 0
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace zln_y_hat_jp = ln_y_hat_`act' if zaction_jp == `act'
	}
	replace zln_y_hat_jp = ln(10400) if zaction_jp == 90
	drop ln_y_hat*
	
	* Drop all synthetic variables *
	keep $keep_vars z*
	
	*******************************
	* STEP 1.1.3: DRAW ACTION j'' *
	*******************************
	disp "***** STEP 1.1.3: DRAW ACTION j'' *****"
	disp "***** Time is now $S_TIME *****"
	
	* Create synthetic variables *
	foreach var in ///
	"action" "Laction" "age" /// 
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* Forward simulate synthetic variables *
	replace Laction = zaction_jp
	replace age = age + 2

	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace exper_`act' = exper_`act' + 1 if zaction_j == `act'
		replace exper_`act' = exper_`act' + 1 if zaction_jp == `act'
	}
	
	* construct synthetic aggregate experience variables *	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}
	
	local counter = 1
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"10" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}
	
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP10 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Pile up the CCPs into a sort of cdf *
	gen a = CCP10
	gen b = a + CCP20
	gen c = b + CCP30
	gen d = c + CCP90
	
	* Draw uniform (0,1) *
	gen draw = runiform()
	
	* Draw action j'' *
	gen zaction_jpp = 0
	replace zaction_jpp = 10 if draw < a
	replace zaction_jpp = 20 if draw >= a & draw < b
	replace zaction_jpp = 30 if draw >= b & draw < c
	replace zaction_jpp = 90 if draw >= c & draw <= 1
	
	* Pick the relevant CCPs *
	gen zCCP_jpp = .
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace zCCP_jpp = CCP`act' if zaction_jpp == `act'
	}

	drop CCP*

	* Bring ln_y_hat matrix using appropriate STER files from first stage *
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		disp "***** Now estimating parameters of the wage equation for action: `act' *****"
		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict ln_y_hat_`act'
		estimates clear
	}
	
	* Pick the relevant ln_y_hats *
	gen zln_y_hat_jpp = 0
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace zln_y_hat_jpp = ln_y_hat_`act' if zaction_jpp == `act'
	}
	replace zln_y_hat_jpp = ln(10400) if zaction_jpp == 90
	drop ln_y_hat*
	
	* Drop all synthetic variables *
	keep $keep_vars z*
	
	******************************
	* STEP 1.1.4: DRAW ACTION cj *
	******************************
	disp "***** STEP 1.1.4: DRAW ACTION cj *****"
	disp "***** Time is now $S_TIME *****"
	
	* Create synthetic variables *
	foreach var in ///
	"action" "Laction" "age" /// 
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* construct synthetic aggregate experience variables *
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}		
	
	local counter = 1
	
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"10" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}

	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP10 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Pick action cj *
	gen zaction_cj = zaction_jp

	* Pick the relevant CCPs *
	gen zCCP_cj = .
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace zCCP_cj = CCP`act' if zaction_cj == `act'
	}
	
	drop CCP*
	
	* Bring ln_y_hat matrix using appropriate STER files from first stage *
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		disp "***** Now estimating parameters of the wage equation for action: `act' *****"
		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict ln_y_hat_`act'
		estimates clear
	}
	
	* Pick the relevant ln_y_hats *
	gen zln_y_hat_cj = 0
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace zln_y_hat_cj = ln_y_hat_`act' if zaction_cj == `act'
	}
	replace zln_y_hat_cj = ln(10400) if zaction_cj == 90
	drop ln_y_hat*
	
	* Drop all synthetic variables *
	keep $keep_vars z*
	
	*******************************
	* STEP 1.1.5: DRAW ACTION cj' *
	*******************************
	disp "***** STEP 1.1.5: DRAW ACTION cj' *****"
	disp "***** Time is now $S_TIME *****"
	
	* Create synthetic variables *
	foreach var in ///
	"action" "Laction" "age" /// 
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* Forward simulate synthetic variables *
	replace Laction = zaction_cj
	replace age = age + 1
	

	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace exper_`act' = exper_`act' + 1 if zaction_cj == `act'
	}
	
	* construct synthetic aggregate experience variables *	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}		
	
	local counter = 1
	
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"10" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}

	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP10 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Choose action *
	gen zaction_cjp = zaction_j

	* Pick the relevant CCPs *
	gen zCCP_cjp = .
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace zCCP_cjp = CCP`act' if zaction_cjp == `act'
	}
	
	drop CCP*
	
	* Bring ln_y_hat matrix using appropriate STER files from first stage *
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		disp "***** Now estimating parameters of the wage equation for action: `act' *****"
		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict ln_y_hat_`act'
		estimates clear
	}
	
	* Pick the relevant ln_y_hats *
	gen zln_y_hat_cjp = 0
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace zln_y_hat_cjp = ln_y_hat_`act' if zaction_cjp == `act'
	}
	replace zln_y_hat_cjp = ln(10400) if zaction_cjp == 90
	drop ln_y_hat*

	* Drop all synthetic variables *
	keep $keep_vars z*
	
	*******************************
	* STEP 1.1.6: DRAW ACTION cj'' *
	*******************************
	disp "***** STEP 1.1.6: DRAW ACTION cj'' *****"
	disp "***** Time is now $S_TIME *****"
	
	* Create synthetic variables *
	foreach var in ///
	"action" "Laction" "age" /// 
	"exper_10" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* Forward simulate synthetic variables *
	replace Laction = zaction_cjp
	replace age = age + 2

	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace exper_`act' = exper_`act' + 1 if zaction_cj == `act'
		replace exper_`act' = exper_`act' + 1 if zaction_cjp == `act'
	}
	
	* construct synthetic aggregate experience variables *	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}
	
	local counter = 1
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"10" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"10" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_B_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}
	
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP10 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Choose action cj'' *
	gen zaction_cjpp = zaction_jpp

	* Pick the relevant CCPs *
	gen zCCP_cjpp = .
	foreach act in ///
	"10" ///
	"20" "30" "90" ///
	{
		replace zCCP_cjpp = CCP`act' if zaction_cjpp == `act'
	}

	drop CCP*

	* Bring ln_y_hat matrix using appropriate STER files from first stage *
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		disp "***** Now estimating parameters of the wage equation for action: `act' *****"
		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_y`act'_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict ln_y_hat_`act'
		estimates clear
	}
	
	* Pick the relevant ln_y_hats *
	gen zln_y_hat_cjpp = 0
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		replace zln_y_hat_cjpp = ln_y_hat_`act' if zaction_cjpp == `act'
	}
	replace zln_y_hat_cjpp = ln(10400) if zaction_cjpp == 90
	drop ln_y_hat*
	
	* Drop all synthetic variables *
	keep $keep_vars z*

	foreach var in ///
	"age" "exper_10" "exper_20" "exper_30" "action" "Laction" ///
	{
		gen `var' = `var'_index
	}
	
	* save intermediate dataset to the data_folder *
	keep pid_int state q age exper_10 exper_20 exper_30 action Laction z*
	order pid_int state q age exper_10 exper_20 exper_30 action Laction z*
	
	local datadir T:\${data_folder}\
	disp "`datadir'"
	compress
	save "`datadir'gc_structural_2nd_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace
	clear

	disp "***** STEP 1: second stage - forward simulation (COMPLETED) *****"
}
disp "***** Finished processing STEP 1 *****"
endtime

****************************************************
* STEP 2: second stage - obtain utility parameters *
****************************************************
disp "***** Started processing STEP 2 *****"
starttime
if 1 == `gate2' ///
{
	disp "***** STEP 2: second stage - obtain utility parameters *****"
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_2nd_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	
	local beta = .90
	display "`beta'"
	*************************************
	* CONSTRUCT LEFT HAND SIDE VARIABLE *
	*************************************
	gen Dep_var = log(zCCP_j/zCCP_cj) + `beta'*log(zCCP_jp/zCCP_cjp) + `beta'*`beta'*log(zCCP_jpp/zCCP_cjpp)

	******************************
	* CONSTRUCT INCOME VARIABLES *
	******************************
	gen z_lny = zln_y_hat_j-zln_y_hat_cj + `beta'*(zln_y_hat_jp-zln_y_hat_cjp) + `beta'*`beta'*(zln_y_hat_jpp-zln_y_hat_cjpp)

	**************************************
	* CONSTRUCT INDICATORS FOR AMENITIES *
	**************************************
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		gen amen`act'_j = 0
		replace amen`act'_j = 1-`beta' if zaction_j == `act'
		gen amen`act'_jp = 0
		replace amen`act'_jp = -1*(1-`beta') if zaction_jp == `act'
		gen zamen`act' = amen`act'_j + amen`act'_jp
		drop amen*
	}

	*******************************************
	* CONSTRUCT INDICATORS FOR MOBILITY COSTS *
	*******************************************
	display "`beta'"
	foreach act in ///
	"10" ///
	"20" "30" ///
	{
		foreach Lact in ///
		"10" ///
		"20" "30" "90" ///
		{
			gen cost_act`Lact'_`act'_z_j = 0
			replace cost_act`Lact'_`act'_z_j = 1 if Laction == `Lact' & zaction_j == `act' & Laction != zaction_j
			gen cost_act`Lact'_`act'_z_jp = 0
			replace cost_act`Lact'_`act'_z_jp = -1 if Laction == `Lact' & zaction_jp == `act' & Laction != zaction_jp
			gen cost_act`Lact'_`act'_j_jp = 0
			replace cost_act`Lact'_`act'_j_jp = `beta' if zaction_j == `Lact' & zaction_jp == `act' & zaction_j != zaction_jp
			gen cost_act`Lact'_`act'_jp_j = 0
			replace cost_act`Lact'_`act'_jp_j = -`beta' if zaction_jp == `Lact' & zaction_j == `act' & zaction_jp != zaction_j
			gen cost_act`Lact'_`act'_jp_jpp = 0
			replace cost_act`Lact'_`act'_jp_jpp = `beta'*`beta' if zaction_jp == `Lact' & zaction_jpp == `act' & zaction_jp != zaction_jpp
			gen cost_act`Lact'_`act'_j_jpp = 0
			replace cost_act`Lact'_`act'_j_jpp = -`beta'*`beta' if zaction_j == `Lact' & zaction_jpp == `act' & zaction_j != zaction_jpp
			gen zcost_act`Lact'_`act' = cost_act`Lact'_`act'_z_j+cost_act`Lact'_`act'_z_jp+cost_act`Lact'_`act'_j_jp+cost_act`Lact'_`act'_jp_j+cost_act`Lact'_`act'_jp_jpp + cost_act`Lact'_`act'_j_jpp
			*gen zcost_act`Lact'_`act'_age = zcost_act`Lact'_`act'*age
			drop cost*
		}
	}

	* save the estimation for vetting *
	local datadir T:\${data_folder}\
	save "`datadir'gc_structural_2nd_util_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_2nd_util_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	
	estimates clear
	reg Dep_var z_lny zamen* i.state#c.zamen20 i.state#c.zamen30 zcost* i.state#c.zcost_act10_20 i.state#c.zcost_act30_20 i.state#c.zcost_act90_20 i.state#c.zcost_act10_30 i.state#c.zcost_act20_30 i.state#c.zcost_act90_30 [aw=q], noc cluster(pid_int)
	local datadir T:\${data_folder}\gc_ster\
	estimates save "`datadir'gc_structural_util_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster", replace
	estimates store util_ols
	estadd ysumm
	local datadir \${output_folder}\
	estout util* using "`datadir'gc_structural_util_B_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.txt", replace ///
	varwidth(12) cells(b(fmt(%9.3f)) ///
	se(star par(`"="("' `")""') fmt(%9.3f))) ///
	starlevels(* 0.1 ** 0.05 *** 0.01) ///
	stats(ymean r2 r2_a N_clust N, ///
	fmt(%9.2f %9.2f %9.2f %9.0g %9.0g) ///
	labels("Mean, dependent variable" "R-squared" "adjusted R-squared" "Number of individuals" "Number of obs."))

	clear
	
	disp "***** STEP 2: second stage - obtain utility parameters (COMPLETED) *****"
}
disp "***** Finished processing STEP 2 *****"
endtime

}
}
********************************************
* STEP X: delete all intermediate datasets *
********************************************
disp "***** Started processing STEP X *****"
starttime
if 1 == `gateX' ///
{
	disp "***** STEP X: delete all intermediate datasets *****"
	local datadir T:\${data_folder}\
	
	disp "***** STEP X: delete all intermediate datasets (COMPLETED) *****"
}
disp "***** Finished processing STEP X *****"
endtime


********
* EXIT *
********
clear all
log close
